program fbc_gen
    use, intrinsic :: iso_fortran_env, only: real64
    implicit none
    integer :: start_int_val, end_int_val, step_int_val
    integer :: i, n, io_status, dof,amplitude_id,fn
    character(len=256) :: filename
    real(real64)::val
    
    print *, "This program is used to generate FBC file as analysis input file."

    ! 获取用户输入
    write(*, '(a)') "Input StartVal EndVal StepVal [use whitespace delimiter]       ="
    read(*, *) start_int_val, end_int_val, step_int_val
    write(*, '(a)') 'Input dof ='
    read(*,'(I2)')  dof
    write(*, '(a)') 'Input amplitude curve id               ='
    read(*,'(I3)') amplitude_id
    write(*, '(a)') 'Input value of force/displacement load ='
    read(*, '(F30.16)') val

    ! 检查输入有效性
    if (step_int_val == 0) then
        write(*, '(a)') "step can not be zero!"
        stop
    end if
    
    ! 计算序列长度
    n = (end_int_val - start_int_val) / step_int_val + 1
    
    ! 生成文件名
    write(filename, '(a,i0,a,i0,a,i0,a,i0,a)') "seq_", start_int_val, "_", end_int_val, "_", step_int_val,'_',dof, ".csv"
    
    ! 打开文件
    open(newunit=fn, file=trim(filename), status='replace', action='write', iostat=io_status)
    if (io_status /= 0) then
        write(*, '(a)') "can create output file!"
        stop
    end if
    
    ! 写入CSV文件
    write(fn, '(a)') "Node Index, Dof, Value"  ! 写入表头
    do i = 0, n-1
        write(fn, '(i12,a,i5,a,i5,a,f30.16)') start_int_val + i*step_int_val,',',dof,',',amplitude_id,',',val
    end do

    ! 关闭文件
    close(fn)
    
    ! 输出完成信息
    write(*, '(i0,a)') n, " records are generated ! " 
    write(*, '(a,a)') "result has been saved in file : ", trim(filename)

end program fbc_gen

